package com.group.EducationalSystem.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.group.EducationalSystem.bean.Root;
import com.group.EducationalSystem.bean.Score;
import com.group.EducationalSystem.bean.Score_tab;
import com.group.EducationalSystem.bean.Student;
import com.group.EducationalSystem.bean.Teacher;
import com.group.EducationalSystem.bean.User;
import com.group.EducationalSystem.mapper.RootMapper;
import com.group.EducationalSystem.tools.Time;
import com.mysql.cj.Session;

@Controller
public class RootController {
	@Resource
	private RootMapper rootmapper;
	
	@RequestMapping("/perfectroot")
	public String perfectStudent(@RequestParam("ID_card") String ID_card,@RequestParam("phone") String phone,@RequestParam("box") String box,@RequestParam("root_name") String root_name,@RequestParam("sex") String sex,@RequestParam("home") String home,HttpSession session,ModelMap map){
		String root_number = ((User)session.getAttribute("user")).getAccount();
		Root root = new Root(root_number,root_name,home,ID_card,sex,phone,box);
		rootmapper.prefectRoot(root);
		session.setAttribute("root",root);
		map.put("image",((User)session.getAttribute("user")).getImage());
		map.put("name", root.getRoot_name());
		return "TemplateRoot";
	}
	
	@RequestMapping("/changeroot")
	public String changeinformation(@RequestParam("changephone") String changephone,@RequestParam("changebox") String changebox,@RequestParam("changehome") String changehome,HttpSession session,ModelMap map){
		rootmapper.changeRoot(changephone, changebox, changehome,((User)session.getAttribute("user")).getAccount());
		map.put("image",((User)session.getAttribute("user")).getImage());
		map.put("name", ((Root)session.getAttribute("root")).getRoot_name());
		return "TemplateRoot";
	}
	
	@PostMapping("/getroot") @ResponseBody
	public int getstudent(HttpServletResponse resp,HttpSession session,ModelMap map){
		//查询数据库
		Root root = rootmapper.getRoot(((User)session.getAttribute("user")).getAccount());
		if(root==null){
			return 1;
		}else{
			return 0;
		}
		
	}
	
	@RequestMapping("root") @ResponseBody
	public int Student(HttpSession session) {
		Root root = rootmapper.getRoot(((User)session.getAttribute("user")).getAccount());
		if(root==null){
			return 1;
		}else{
			return 0;
		}
	}
	
	@ResponseBody
	@RequestMapping("/select_student_information")
	public List<Student> queryachievement(HttpServletResponse resp,HttpSession session){
		List<Student> list = rootmapper.getStudent_information();
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/getStudentByinformation")
	public void getCoursesByName(@RequestParam("name") String name,@RequestParam("account") String account,HttpSession session,HttpServletResponse resp){
		session.setAttribute("name", name);
		session.setAttribute("account", account);
	}
	
	@ResponseBody
	@RequestMapping("/getStudentByNameOrAccount")
	public List<Student> getStudentByNameOrAccount(HttpServletResponse resp,HttpSession session){
		String name = session.getAttribute("name").toString();
		String account = session.getAttribute("account").toString();
		List<Student> list = null;
		if((name.equals("")||name==null)&&(!account.equals(""))) {
			Student student = rootmapper.getStudentByAccount(account);
			if(student!=null) {
				list=new ArrayList<Student>();
				list.add(student);
			}else {
				list=new ArrayList<Student>();
				return list;
			}
		}else if((account.equals("")||account==null)&&(!name.equals(""))) {
			list = rootmapper.getStudentByName(name);
		}else if((!name.equals(""))&&(!account.equals(""))) {
			list = rootmapper.getStudentByNameOrAccount(account, name);
		}else {
			list=rootmapper.getStudent_information();
		}
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/deleteStudent")
	public void deleteStudent(@RequestParam("student_id") String student_id) {
		Student student = rootmapper.selectStudent(student_id);
		rootmapper.delstudent(student_id);
		rootmapper.deluser(student.getStudent_number());
		rootmapper.del_student_score(student_id);
	}
	
	
	@ResponseBody
	@RequestMapping("/getTeacherInformation")
	public List<Teacher> getTeacherInformation(HttpServletResponse resp,HttpSession session){
		List<Teacher> list = rootmapper.getTeacherInformation();
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/deleteTeacher")
	public void deleteTeacher(@RequestParam("teacher_id") String teahcer_id) {
		rootmapper.delteacher(teahcer_id);
	}
	
	@ResponseBody
	@RequestMapping("/getTeacherByinformation")
	public void getTeacherByinformation(@RequestParam("teacher_name") String teacher_name,@RequestParam("teacher_account") String teacher_account,HttpSession session,HttpServletResponse resp){
		session.setAttribute("teacher_name", teacher_name);
		session.setAttribute("teacher_account", teacher_account);
	}
	
	@ResponseBody
	@RequestMapping("/getTeacherByNameOrAccount")
	public List<Teacher> getTeacherByNameOrAccount(HttpServletResponse resp,HttpSession session){
		String name = session.getAttribute("teacher_name").toString();
		String account = session.getAttribute("teacher_account").toString();
		List<Teacher> list = null;
		if((name.equals("")||name==null)&&(!account.equals(""))) {
			list = rootmapper.getTeacherByAccount(account);
		}else if((account.equals("")||account==null)&&(!name.equals(""))) {
			list = rootmapper.getTeacherByName(name);
		}else if((!name.equals(""))&&(!account.equals(""))) {
			list = rootmapper.getTeacherByNameOrAccount(account, name);
		}else {
			list=rootmapper.getTeacherInformation();
		}
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/getStudentAchievement")
	public List<Score> getStudentAchievement(HttpServletResponse resp,HttpSession session){
		List<Score> list = rootmapper.getScore_tab();
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/getScoreByinformation")
	public void getScoreByinformation(@RequestParam("name2") String name2,@RequestParam("course_name") String course_name,HttpSession session,HttpServletResponse resp){
		session.setAttribute("name2", name2);
		session.setAttribute("course_name", course_name);
	}
	
	@ResponseBody
	@RequestMapping("/getScoreByName")
	public List<Score> getScoreByName(HttpServletResponse resp,HttpSession session){
		String name = session.getAttribute("name2").toString();
		String course_name = session.getAttribute("course_name").toString();
		List<Score> list = null;
		if((name.equals("")||name==null)&&(!course_name.equals(""))) {
			list = rootmapper.getScore_tabBycourse_name(course_name);
		}else if((course_name.equals("")||course_name==null)&&(!name.equals(""))) {
			list = rootmapper.getScore_tabBystudent_name(name);
		}else if((!name.equals(""))&&(!course_name.equals(""))) {
			list = rootmapper.getScore_tabByName(name, course_name);
		}else {
			list=rootmapper.getScore_tab();
		}
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/getuserInformation")
	public List<User> getuserInformation(HttpServletResponse resp,HttpSession session){
		List<User> list = rootmapper.getUser();
		return list;
	}
	
	@ResponseBody
	@PostMapping("/deleteUser")
	public int deleteUser(@RequestParam("user_id") String user_id) {
		User user = rootmapper.getUserbyId(user_id);
		rootmapper.delUser(user_id);
		if(user.getPower()==1) {
			Student student = rootmapper.getStudentByAccount(user.getAccount());
			if(student!=null) {
				rootmapper.delStudentByaccount(user.getAccount());
				rootmapper.del_student_score(student.getStudent_id()+"");
			}
			return 1;
		}else if(user.getPower()==2) {
			rootmapper.delteacherByAccount(user.getAccount());
			return 1;
		}else if(user.getPower()==3) {
			return 0;
		}else {
			return 0;
		}
	}
	
	@ResponseBody
	@PostMapping("/addUser")
	public int addUser(@RequestParam("user_account") String user_account,@RequestParam("user_power")String user_power) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String data = df.format(new Date());
        String useraccount=data+user_account;
        rootmapper.addUser(useraccount, useraccount, user_power, "https://img0.baidu.com/it/u=1986451467,394304688&fm=26&fmt=auto&gp=0.jpg");
		return 1;
	}
}
